"""
人工神经网络（Artificial Neural Network，ANN），通常简称为神经网络，是深度学习的基础，它是受到人类大脑结构启发而诞生的一种算法。
人类正是通过相同的冲动反复地刺激神经元，改变神经元之间的链接的强度来进行学习。

神经网络算法试图模拟生物神经系统的学习过程，以此实现强大的预测性能。


"""

import numpy as np
from sklearn.neural_network import MLPClassifier as DNN
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score as cv
import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier as DTC
from sklearn.model_selection import train_test_split as TTS
from time import time
import datetime
data = load_breast_cancer()
X = data.data
y = data.target
Xtrain, Xtest, Ytrain, Ytest = TTS(X,y,test_size=0.3,random_state=420)

# times = time()
# dnn = DNN(hidden_layer_sizes=(100,),random_state=420)
# print(cv(dnn,X,y,cv=5).mean())
# print(time() - times)
#
# #使用决策树进行一个对比
# times = time()
# clf = DTC(random_state=420)
# print(cv(clf,X,y,cv=5).mean())
# print(time() - times)

dnn = DNN(hidden_layer_sizes=(100,),random_state=420).fit(Xtrain,Ytrain)
print(dnn.score(Xtest,Ytest))
#使用重要参数n_layers_
print(dnn.n_layers_)
#可见，默认层数是三层，由于必须要有输入和输出层，所以默认其实就只有一层隐藏层


#来试试看学习曲线
# s = []
# for i in range(100,2000,100):
#     dnn = DNN(hidden_layer_sizes=(int(i),),random_state=420).fit(Xtrain,Ytrain)
#     s.append(dnn.score(Xtest,Ytest))
# print(i,max(s))
# plt.figure(figsize=(20,5))
# plt.plot(range(100,2000,100),s)
# plt.show()

#那如果增加隐藏层，控制神经元个数，会发生什么呢？
# s = []
# layers = [(100,),(100,100),(100,100,100),(100,100,100,100),(100,100,100,100,100),
# (100,100,100,100,100,100)]
# for i in layers:
#     dnn = DNN(hidden_layer_sizes=(i),random_state=420).fit(Xtrain,Ytrain)
#     s.append(dnn.score(Xtest,Ytest))
# print(i,max(s))
# plt.figure(figsize=(20,5))
# plt.plot(range(3,9),s)
# plt.xticks([3,4,5,6,7,8])
# plt.xlabel("Total number of layers")
# plt.show()


#如果同时增加隐藏层和神经元个数，会发生什么呢？
s = []
layers = [(100,),(150,150),(200,200,200),(300,300,300,300)]
for i in layers:
    dnn = DNN(hidden_layer_sizes=(i),random_state=420).fit(Xtrain,Ytrain)
    s.append(dnn.score(Xtest,Ytest))
print(i,max(s))
plt.figure(figsize=(20,5))
plt.plot(range(3,7),s)
plt.xticks([3,4,5,6])
plt.xlabel("Total number of layers")
plt.show()

"""

"""